package com.fansz.FanszAPI.service.impl;

import com.fansz.FanszAPI.domain.Friends;
import com.fansz.FanszAPI.utils.Constants;
import com.fansz.FanszAPI.utils.StringUtils;
import com.mongodb.WriteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Order;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.fansz.FanszAPI.domain.User;
import com.fansz.FanszAPI.service.UserService;

import java.util.List;
import java.util.regex.Pattern;

@Service
public class UserServiceImpl implements UserService
{
    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public void saveUser(User user)
    {
        // TODO Auto-generated method stub
        mongoTemplate.save(user, "user");
    }

    @Override
    public User findByName(String name, String type)
    {
        // TODO Auto-generated method stub
        if(!StringUtils.isEmpty(name) && !StringUtils.isEmpty(type))
        {
            if (Constants.USER_TYPE_MOBILE.equals(type))
            {
                return mongoTemplate.findOne(new Query(Criteria.where("mobile").is(name)), User.class, "user");
            }
            else
            {
                return mongoTemplate.findOne(new Query(Criteria.where("mail").is(name)), User.class, "user");
            }
        }
        else
        {
            return null;
        }
    }

    @Override
    public User findByUserId(String userId)
    {
        // TODO Auto-generated method stub
        if(!StringUtils.isEmpty(userId))
        {
            return mongoTemplate.findOne(new Query(Criteria.where("id").is(userId)), User.class, "user");
        }
        else
        {
            return null;
        }

    }

    @Override
    public List<User> findByUserIds(List<String> userIds)
    {
        // TODO Auto-generated method stub
        if(!StringUtils.isEmpty(userIds))
        {
            return mongoTemplate.find(new Query(Criteria.where("id").in(userIds)), User.class, "user");
        }
        else
        {
            return null;
        }

    }

    @Override
    public WriteResult updatePassword(String mobile,String password)
    {
        // TODO Auto-generated method stub
        Query query = new Query(Criteria.where("mobile").is(mobile));
        Update update = new Update();
        update.set("password", password);
        WriteResult wr = mongoTemplate.updateFirst(query, update, User.class);
        return wr;
    }

    @Override
    public List<User> searchFriend(String key, int begin, int count)
    {
        Query query = new Query();
        query.addCriteria(Criteria.where(Constants.MOBILE).regex(".*" + key + ".*"));
        query.skip(begin);
        query.limit(count);
        query.with(new Sort(new Sort.Order(Constants.MOBILE)));
        return mongoTemplate.find(query, User.class, "user");
    }

    @Override
    public long searchFriendCount(String key, int begin)
    {
        Query query = new Query();
        query.addCriteria(Criteria.where(Constants.MOBILE).regex(".*" + key + ".*"));
        query.skip(begin);
        query.with(new Sort(new Sort.Order(Constants.MOBILE)));

        return mongoTemplate.find(query, User.class, "user").size();
    }

    @Override
    public List<Friends> getFriends(String id, String status)
    {
        Query query = new Query();
        query.addCriteria(Criteria.where(Constants.FRIENDS_USER_AGREE).is(id));
        query.addCriteria(Criteria.where(Constants.FRIENDS_STATUS).is(status));
        query.with(new Sort(new Sort.Order(Constants.FRIENDS_CREATE_TIME)));
        return mongoTemplate.find(query, Friends.class, "friends");
    }

    @Override
    public Friends getFollow(String id, String followId)
    {
        Query query = new Query();
        query.addCriteria(Criteria.where(Constants.FRIENDS_USER_ADD).is(id));
        query.addCriteria(Criteria.where(Constants.FRIENDS_USER_AGREE).is(followId));
        return mongoTemplate.findOne(query, Friends.class, "friends");
    }

    @Override
    public WriteResult updateFriendsStatus(String id,String status)
    {
        // TODO Auto-generated method stub
        Query query = new Query(Criteria.where("uid").is(id));
        Update update = new Update();
        update.set("status", status);
        update.set("agreeTime", System.currentTimeMillis());
        WriteResult wr = mongoTemplate.updateFirst(query, update, Friends.class);
        return wr;
    }

    @Override
    public WriteResult updateFriendsInfo(String id,String followId, String status)
    {
        // TODO Auto-generated method stub
        Query query = new Query(Criteria.where("userIdAdd").is(id));
        query.addCriteria(Criteria.where("userIdAgree").is(followId));
        Update update = new Update();
        update.set("status", status);
        WriteResult wr = mongoTemplate.updateFirst(query, update, Friends.class);
        return wr;
    }

    @Override
    public void saveFriends(Friends friends)
    {
        // TODO Auto-generated method stub
        mongoTemplate.save(friends, "friends");
    }
}
